<?php

class SupplierInventoriesController extends AppController {

    var $name = 'SupplierInventories';
    var $pageTitle = 'Customer Inventory';
    var $viewPath = 'suppliers/delivery/supplier_inventories';
    var $uses = array('CustProductSite', 'Site', 'Customer', 'Supplier', 'Category');
    /**
     * สำหรับ Site user ที่มีสิทธิ์ can_view_other_site เลือกดูข้อมูล site อื่น
     * @var int site id ที่ต้องการดู
     */
    var $viewSiteInventoryId = null;

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('parentNav', 'supplier_inventories');
    }

    function _getSupplierId() {
        // Customer เป็นคนดู ส่ง Supplier_id ที่ต้องการดูมาใน named params
        if (!empty($this->passedArgs['supplier_id'])) {
            $supplier_id = $this->passedArgs['supplier_id'];
        } else {
            $supplier_id = $this->Auth->user('supplier_id');
        }
        return $supplier_id;
    }

    /**
     * ใช้ทั้งฝั่ง Supplier และ Customer ในการดูคลังลูกค้าที่ Supplier
     * @param type $customer_id
     * @param type $site_id
     */
    function display($customer_id, $site_id = null) {
        parent::display('CustProductSite');

        $supplier_id = $this->_getSupplierId();

        if ($site_id == null) {
            $site = $this->Site->findByCustomerId($customer_id);
            $site_id = $site['Site']['id'];
        }

        $op = array(
            'supplier_part_no' => 'LIKE',
            'category_id' => '=',
            'product_id' => '=',
            'brand_id' => '=',
            'model' => 'LIKE',
            'title' => 'LIKE',
            'text' => 'LIKE',
            'supplier_part_no' => 'LIKE',
            'stock_code' => 'LIKE',
            'supplier_current_stock' => !empty($this->data['Operand']['supplier_current_stock']) ? $this->data['Operand']['supplier_current_stock'] : '>',
            'reserve_qty' => !empty($this->data['Operand']['reserve_qty']) ? $this->data['Operand']['reserve_qty'] : '>',
            'min_stock' => !empty($this->data['Operand']['min_stock']) ? $this->data['Operand']['min_stock'] : '>',
            'max_stock' => !empty($this->data['Operand']['max_stock']) ? $this->data['Operand']['max_stock'] : '>',
        );

        $this->paginate['CustProductSite']['conditions']['CustProductSite.customer_id'] = $customer_id;
        $this->paginate['CustProductSite']['conditions']['CustProductSite.supplier_id'] = $supplier_id;
        $this->paginate['CustProductSite']['contain'] = array('Product' => array('Category', 'Brand'));

        if ($this->Auth->user('prefix') == User::USER_PREFIX_CUSTOMER) {
            $this->pageTitle = 'Vendor Inventory';
        }

        $this->set('supplier_id', $supplier_id);
        $this->Supplier->contain();
        $this->set('supplier', $this->Supplier->findById($supplier_id));

        $this->Customer->contain();
        $this->set('customer', $this->Customer->findById($customer_id));
        $this->set('customer_id', $customer_id);

        $this->Site->contain();
        $this->set('site', $this->Site->findById($site_id));
        $this->set('site_id', $site_id);
//        debug($this->data);
        $this->_paging('CustProductSite', $op);
    }

    function view($id) {
        if ($id != null) {
            $this->CustProductSite->contain(array('Product' => array('Category', 'Brand', 'Unit'), 'ShortestPeriod', 'MaintenancePeriod', 'CyclePeriod', 'Supplier'));
            $this->data = $this->CustProductSite->findById($id);

            $this->_neighbors($id, $this->CustProductSite, array(
                'field' => 'stock_code', 'value' => $this->data['CustProductSite']['stock_code'],
                'fields' => array('id', 'stock_code'),
                'contain' => array('Site',
                    'Product.Supplier', 'Product.Category', 'Product.Brand', 'Product.supplier_part_no', 'Product.part_no', 'Product.model', 'Product.title', 'Product.text',
                    'ProductSerial' => array('CustLocation', 'limit' => 1, 'order' => 'stock_out_priority')
                    ))
            );

            $this->set('customer_id', $this->data['CustProductSite']['customer_id']);
            $this->set('site_id', $this->data['CustProductSite']['site_id']);
            $this->set('supplier_id', $this->data['CustProductSite']['supplier_id']);
        }
    }

    function popup_search() {
        parent::popup_search();

        $supplier_id = $this->_getSupplierId();
        $customer_id = $this->passedArgs['customer_id'];
        $site_id = $this->passedArgs['site_id'];
  
        $this->set('operands', array('<' => '<', '<=' => '<=', '=' => '=', '>' => '>', '>=' => '>='));
        $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
        $this->set('popupWidth', 800);
        $this->set('params', $customer_id . '/' . $site_id . '/supplier_id:' . $supplier_id);
    }

    /**
     * popup นี้จะเหมือนหน้า display เอามาแสดงเป็น popup สามารถเลือก product แล้วส่ง part no กลับไปหน้าที่เรียกมาได้
     */
    function popup_view() {
        $this->layout = 'popup';
        // clear session การ search จากครั้งก่อน
        $this->Session->delete('Product');
        $this->Session->delete('Products');
        $this->Session->delete("CustProductSite.paging.site_id");
        $this->Session->delete("CustProductSite.paging.customer_id");
        $supplier_id = $this->Auth->user('supplier_id');

        // ถ้ามีการส่ง site_id เข้ามา เพิ่มในเงื่อนไขการ search เลย
        if (isset($this->passedArgs['site_id'])) {
            $site_id = $this->passedArgs['site_id'];
            $this->Site->id = $site_id;
            $customer_id = $this->Site->field('customer_id');
            $this->set('sites', $this->Site->find('list', array('order' => 'customer_id, name', 'conditions' => array('customer_id' => $customer_id))));
        } else {
            $customer_id = null;
            $site_id = null;
            $this->set('sites', null);
        }

        $this->set('popupTitle', 'Select a Product');
        $this->set('popupWidth', '$j(document).width() - 100');

        // id ที่จะถูกเลือกเป็น default
        $this->set('site_id', $site_id);
        $this->set('customer_id', $customer_id);

        $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
        $this->set('brands', $this->Product->Brand->find('list', array('conditions' => array('supplier_id' => $supplier_id))));
        $this->set('customers', $this->VSCustomer->findListByCustomerOnHand($this->Auth->user('id'), $supplier_id));
    }

    /**
     * ใช้ร่วมกับ popup_view เป็นส่วนตารางแสดงแถว customers
     */
    function popup_paginate() {
        $this->Product->bindModel(array('hasMany' => array('CustProductSite')));
        if ($this->Session->check("{$this->name}.site_id")) {
            $this->data['CustProductSite']['site_id'] = $this->Session->read("{$this->name}.site_id");
        }

        if ($this->Session->check("{$this->name}.customer_id")) {
            $this->data['CustProductSite']['customer_id'] = $this->Session->read("{$this->name}.customer_id");
        }

        if (isset($this->passedArgs['site_id'])) {
            $this->data['CustProductSite']['site_id'] = $this->passedArgs['site_id'];
            $this->Session->write("{$this->name}.site_id", $this->data['CustProductSite']['site_id']);
        }
        if (isset($this->passedArgs['customer_id'])) {
            $this->data['CustProductSite']['customer_id'] = $this->passedArgs['customer_id'];
            $this->Session->write("{$this->name}.customer_id", $this->data['CustProductSite']['customer_id']);
        }

        $this->display();
    }
}
?>